<?php
/* CONFIGURATION */
//error_reporting(0);

/* BASIC AUTHENTICATION CHECK */

if (!isset($_SESSION["adminid"]) || !defined('ROOTDIR'))
{
	exit();
}

/* INITIALIZING COMMON VARIABLES */

/* check for admin privileges */
// require Config Products privileges to edit items
if(mysql_num_rows(mysql_query("SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid WHERE permid=71 AND tbladmins.id=". $_SESSION["adminid"]))) 
{
	$editable = 1;
}

/* END OF INITIALIZING COMMON VARIABLES */

/* PROCESSING POST/GET REQUEST */
if ($_POST["tag"] && $editable) 
{
		foreach ($_POST["tag"] AS $id=>$tag) {
			$id = sanitize($id);
                        $tag = sanitize($_POST["tag"]["$id"]);
                        $specs =  sanitize($_POST["specs"]["$id"]);
                        $dc =  sanitize($_POST["dc"]["$id"]);
                        $rack =  sanitize($_POST["rack"]["$id"]);
                        $name =  sanitize($_POST["name"]["$id"]);
                        $mainip =  sanitize($_POST["mainip"]["$id"]);
                        $otherips =  sanitize($_POST["otherips"]["$id"]);
                        $customer =  sanitize($_POST["customer"]["$id"]);
                        $note = sanitize($_POST["note"]["$id"]);
			update_query("mod_servermanager",array("tag"=>$tag, "specs"=>$specs, "dc"=>$dc, "rack"=>$rack, "name"=>$name, "mainip"=>$mainip, "otherips"=>$otherips, "customer"=>$customer, "note"=>$note),"id='$id'");
		}
} 
elseif ($_POST["newtag"] && $editable) 
{
		$newtag = sanitize($_POST["newtag"]);
                $newspecs =  sanitize($_POST["newspecs"]);
                $newdc =  sanitize($_POST["newdc"]);
                $newrack =  sanitize($_POST["newrack"]);
                $newname =  sanitize($_POST["newname"]);
                $newmainip =  sanitize($_POST["newmainip"]);
                $newotherips =  sanitize($_POST["newotherips"]);
                $newcustomer =  sanitize($_POST["newcustomer"]);
		$newnote = sanitize($_POST["newnote"]);
		insert_query("mod_servermanager",array("tag"=>$newtag, "specs"=>$newspecs, "dc"=>$newdc, "rack"=>$newrack, "name"=>$newname, "mainip"=>$newmainip, "otherips"=>$newotherips, "customer"=>$newcustomer, "note"=>$newnote));
} 
elseif ($_GET["delete"] && $editable) 
{
		$id = sanitize($_GET["id"]);
		delete_query("mod_servermanager","id='$id'");



}
elseif (($_POST["tag"] OR $_POST["newtag"] OR $_GET["delete"]) && $editable != 1)
{
	$message = "You do not have permissions to perform this action. Permission required: Configure Products/Services";
}




if ($message) 
	echo "<div id='infobox'>".$message."</div>";
$tab=1;
?>
<!-- javascript for tab and input validation -->
<script type="text/javascript" src="../modules/admin/developer/ibox/ibox.js"></script>
<script type="text/javascript">
iBox.setPath("ibox/");iBox.fade_in_speed=5;iBox.fade_out_speed=5;
/* tab control */
$(document).ready(function(){$(".tabbox").css("display","none");var selectedTab;$(".tab").click(function(){var elid=$(this).attr("id");$(".tab").removeClass("tabselected");$("#"+elid).addClass("tabselected");$(".tabbox").slideUp();if(elid!=selectedTab){selectedTab=elid;$("#"+elid+"box").slideDown();}else{selectedTab=null;$(".tab").removeClass("tabselected");}$("#tab").val(elid.substr(3));});$("#checkall").toggle(function(){$(".checkall").attr("checked","checked");},function(){$(".checkall").attr("checked","");});
<?php if (isset($tab))	echo 'selectedTab = "tab'.$tab.'";$("#tab'.$tab.'").addClass("tabselected");$("#tab'.$tab.'box").css("display","");'; ?>
});
/* only allow enter numeric value into a field */
function numeric_only(e){var key;var keychar;if(window.event){key=window.event.keyCode;}else if(e){key=e.which;}else{return true;}keychar=String.fromCharCode(key);if((key==null)||(key==0)||(key==8)||(key==9)||(key==13)||(key==27)){return true;}else if((("0123456789").indexOf(keychar)>-1)){return true;}else{return false;}};

function doDelete(id){if (confirm("Are you sure you want to delete this server info?")){window.location="'<?php echo $modulelink;?>'&delete=true&id="+id;}};
</script>
<div id="tabs">
	<ul>
		<li id="tab0" class="tab"><a href="javascript:;">Search/Filter</a></li>
		<li id="tab1" class="tab"><a href="javascript:;">Create</a></li>
	</ul>
</div>
<div id="tab0box" class="tabbox">
	<div id="tab_content">
		Not yet implemented!
	</div>
</div>
<div id="tab1box" class="tabbox">
	<div id="tab_content">
		<form method="post" action="<?php echo $modulelink;?>">
			<table class="form" width="100%" border="0" cellspacing="2" cellpadding="3">
				<tr>
					<td width="10%" class="fieldlabel">Server Tag</td>
					<td width="40%" class="fieldarea"><input type="text" name="newtag" size="60"></td>
					<td width="10%" class="fieldlabel">Data Center</td>
					<td width="40%" class="fieldarea">
						<select name="newdc">
							<option value="tag">Server Tag</option>
						</select>
					</td>
				</tr>
				<tr>
					<td width="10%" class="fieldlabel">CPU</td>
					<td width="40%" class="fieldarea">
						<select name="newcpu">
						<?php
						$r = mysql_query("SELECT id, val FROM mod_config WHERE module='node_manager' AND config='cpus';");
						while ($cpus = mysql_fetch_assoc($r))
						{
							echo '<option value="'.$cpus["id"].'">'.$cpus["val"].'</option>';
						}
						?>
						</select>
						x 
						<select name="cpucnt">
							<option value="0">0</option>
							<option value="1">1</option>
						</select>
					</td>
					<td width="10%" class="fieldlabel">Main</td>
					<td width="40%" class="fieldarea">
						<select name="newmain">
							<option value="tag">Server Tag</option>
						</select>
					</td>					
				</tr>
				<tr>					
					<td width="10%" class="fieldlabel">HDD</td>
					<td width="40%" class="fieldarea">
						<select name="newhdd0">
							<option value="tag">Server Tag</option>
						</select>
						x <input type="text" name="hddnum0" size="1" onKeyPress="return numeric_only(event);">
						<br/>
						<select name="newhdd1">
							<option value="tag">Server Tag</option>
						</select>
						x <input type="text" name="hddnum1" size="1" onKeyPress="return numeric_only(event);">
					</td>			
					<td width="10%" class="fieldlabel">RAM</td>
					<td width="40%" class="fieldarea">
						<select name="newram0">
							<option value="tag">Server Tag</option>
						</select>
						x <input type="text" name="ramnum0" size="1">
						<br/>
						<select name="newram1">
							<option value="tag">Server Tag</option>
						</select>
						x <input type="text" name="ramnum1" size="1">						
					</td>					
				</tr>
				
				<tr>
					<td align="right">Rack, row</td>
					<td><input type="text" name="newrack" style="width:100%" id="newrack" /></td>
					<td align="right">Customer</td>
					<td><input type="text" name="newcustomer" style="width:100%" id="newcustomer" /></td>
				</tr>
				<tr>
					<td align="right">Note</td>
					<td><textarea name="newnote"  rows="3" style="width:100%" id="newnote" /></textarea></td>
					<td>&nbsp;</td>
					<td><input type="submit" value="Add New Server"></td>
				</tr>
			</table>
		</form>
	</div>
</div>
<?php

/*
echo '<form method="post" action="'.$modulelink.'">
<p align="center">Search for <select name="filterfield">
<option value="tag"';
if ($filterfield=="tag") { echo ' selected'; }
echo '>Server Tag</option>
<option value="dc"';
if ($filterfield=="dc") { echo ' selected'; }
echo '>Data Center</option>
<option value="rack"';
if ($filterfield=="rack") { echo ' selected'; }
echo '>Rack, row</option>
<option value="name"';
if ($filterfield=="name") { echo ' selected'; }
echo '>Hostname</option>
<option value="mainip"';
if ($filterfield=="mainip") { echo ' selected'; }
echo '>Main IP</option>
<option value="otherips"';
if ($filterfield=="otherips") { echo ' selected'; }
echo '>Other IPs</option>
<option value="Hostname"';
if ($filterfield=="note") { echo ' selected'; }
echo '>Note</option>
</select> that <select name="filtertype">
<option';
if ($filtertype=="contains") { echo ' selected'; }
echo '>contains</option>
<option';
if ($filtertype=="starts with") { echo ' selected'; }
echo '>starts with</option>
<option';
if ($filtertype=="ends with") { echo ' selected'; }
echo '>ends with</option>
</select> <input type="text" name="filtervalue" size="30" value="'.$filtervalue.'"> <input type="submit" value="Filter">
</form>&nbsp;&nbsp;&nbsp;&nbsp;';
if ($editable) 
	echo '<a href="#newserver" style="text-decoration:none" rel="ibox&width=600" title="Add New Server"><input type="button" name="button" id="button" value="Add New Server" /></a>';
echo '</p>
<table style="text-align:center;" width="100%" cellspacing="1" bgcolor="#cccccc"><tr bgcolor="#efefef" style="text-align:center;font-weight:bold;"><td width="100">Server Tag</td><td width="80">DC</td><td width="60">Rack, Row</td><td width="100">Hostname</td><td width="80">Main IP</td><td width="200">Other IPs</td><td width="30">Client</td>';
if ($editable) 
	echo '<td width="30">Info</td><td width="20"></td>';
echo '</tr>';
	$id="";
	$query = "SELECT * FROM mod_servermanager";
	if ($filterfield) {
		$query.= " WHERE $filterfield";
		if ($filtertype=="starts with") {
			$query.= " LIKE '$filtervalue%'";
		} elseif ($filtertype=="ends with") {
			$query.= " LIKE '%$filtervalue'";
		} else {
			$query.= " LIKE '%$filtervalue%'";
		}
	}
	$query.= " ORDER BY tag ASC";
	$result=mysql_query($query);
	while ($data = mysql_fetch_array($result)) {
		$id = $data["id"];
                $client = ($data["customer"])? '<a href="clientshosting.php?'. $data["customer"] .'" target="_blank">View</a>' : '<b>Free</b>'; 
		echo '<tr bgcolor="#ffffff"><td>'. $data["tag"] .'</td>
                      <td>'. $data["dc"] .'</td>
                      <td>'. $data["rack"] .'</td>
                      <td>'. $data["name"] .'</td>
                      <td>'. $data["mainip"] .'</td>
                      <td>'. $data["otherips"] .'</td>
                      <td>'. $client .'</td>';
		if ($editable)
			echo '    <td><a href="#server'. $id .'" rel="ibox&width=70%" title="Server Info">Edit</a></td>
                      <td align="center"><a href="#" onClick="doDelete(\''.$id.'\');return false"><img src="images/delete.gif" width="16" height="16" border="0" alt="Delete"></a></td>';
		echo '</tr>';

               $hidden .= '<div id="server'. $id .'" style="display:none;"><form method="post" action="'.$modulelink.'">
                    <table style="text-align:center;" width="100%" cellspacing="1" bgcolor="#cccccc">
                      <tr>
                        <td width="16%" align="right">Server Tag</td>
                        <td width="34%"><input type="text" name="tag['.$id.']" style="width:100%" value="'. $data["tag"] .'"></td>
                      <td width="16%" align="right">Main IP</td>
                      <td width="34%"><input type="text" name="mainip['.$id.']" style="width:100%" value="'. $data["mainip"] .'"></td>
                      </tr>
                      <tr><td align="right">Specs</td>
                      <td><textarea name="specs['.$id.']" rows="3" style="width:100%">'. $data["specs"] .'</textarea></td>
                      <td align="right">Other IPs</td>
                      <td><textarea name="otherips['.$id.']" rows="3" style="width:100%">'. $data["otherips"] .'</textarea></td>
                      </tr>
                      <tr><td align="right">Data Center</td>
                      <td><input type="text" name="dc['.$id.']" style="width:100%" value="'. $data["dc"] .'"></td>
                      <td align="right">Hostname</td>
                      <td><input type="text" name="name['.$id.']" style="width:100%" value="'. $data["name"] .'"></td>
                      </tr>
                      <tr><td align="right">Rack, row</td>
                      <td><input type="text" name="rack['.$id.']" style="width:100%" value="'. $data["rack"] .'"></td>
                      <td align="right">Customer</td>
                      <td align="left"><input type="text" name="customer['.$id.']" style="width:70%" value="'. $data["customer"] .'"> <a href="clientshosting.php?'. $data["customer"] .'" target="_blank">View</a> </td>
                      </tr>
                      <tr><td align="right">Note</td>
                      <td><textarea name="note['.$id.']" rows="3" style="width:100%">'. $data["note"] .'</textarea></td>
                      <td>&nbsp;</td>
                      <td><input type="submit" value="Submit"></td>
</tr></table></form></div>';            
	}
	if (!$id) {
		echo '<tr bgcolor="#ffffff"><td colspan="9" align="center">No Data Found</td></tr>';
	}
	echo '
</table>
'. $hidden .'
<div id="newserver" style="display:none;">

</div>
';*/
?>
